package com.lbl.third.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbl.third.common.CustomException;
import com.lbl.third.enytity.Category;
import com.lbl.third.enytity.Dish;
import com.lbl.third.enytity.Setmeal;
import com.lbl.third.mapper.CateGoryMapper;
import com.lbl.third.service.CateGoryService;
import com.lbl.third.service.DishService;
import com.lbl.third.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CateGoryServiceImpl extends ServiceImpl<CateGoryMapper,Category> implements CateGoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    /**
     * 根据id判断分类，删除之前需要进行判断是否分类下有子项目方法
     * @param id
     */
    @Override
    public void remove(Long id) {

        //查询当前分类下是否关联了套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类Id进行查询
        queryWrapper.eq(Dish::getCategoryId, id);
        int count0 = dishService.count(queryWrapper);
        if (count0 > 0) {
            //已经关联套餐，抛出一个业务异常，并进入异常处理器
            throw new CustomException("当下分类下关联了菜品，无法删除");
        }

        //查询当前分类下是否关联了套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类Id进行查询
        queryWrapper1.eq(Setmeal::getCategoryId, id);
        int count1 = setmealService.count(queryWrapper1);
        if (count1 > 0) {
            //已经关联套餐，抛出一个业务异常，并进入异常处理器
            throw new CustomException("当下分类下关联了套餐，无法删除");
        }

        //正常删除业务
        super.removeById(id);
    }
}




